package com.nami.gate.middleware.mq.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;

import com.nami.gate.cache.LocalChannelCache;
import com.nami.gate.external.basic.constants.DownTopicConst;
import com.nami.gate.external.basic.request.down.DownMsgLogout;
import com.nami.gate.utils.JsonUtils;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DownMsgLogOutConsumer extends AbstractConsumer {

    private static final Logger log = LoggerFactory.getLogger(DownMsgLogOutConsumer.class);

    @Override
    public void onMessage(byte[] body) {
        try {
            TimeInterval timer = DateUtil.timer();

            DownMsgLogout logout = JsonUtils.parseObject(body, DownMsgLogout.class);
            Channel channel = LocalChannelCache.get(logout.getSessionId());
            channel.writeAndFlush(logout.getPacket());
            // 删除L1Cache
            LocalChannelCache.removeAll(logout.getSessionId());
            log.info("[logout] userId:{}, use:{}ms", logout.getSessionId(), timer.interval());
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("[logout] 消费出现问题:{}", var3.getMessage());
        }

    }

    @Override
    public String getTopicName() {
        return DownTopicConst.LOGOUT_TOPIC;
    }

}


